﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MultipleQueue.Distributions;

namespace MultipleQueue
{
    public partial class ServiceTimeDistribution : Form
    {
        private int servers;
        private List<IDistribution> serviceTimeDist;
        public ServiceTimeDistribution(int serversNum)
        {
            InitializeComponent();
            servers = serversNum;
            serviceTimeDist = new List<IDistribution>();
            lblServerNumber.Text = "Server: 1";
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            this.dataGridServiceTime.Enabled = true;
            this.txtExpMeanService.Enabled = false;
            this.txtMaxService.Enabled = false;
            this.txtMinService.Enabled = false;

            DataTable serviceTime = new DataTable();
            serviceTime.Columns.Add("Service Time values");
            serviceTime.Columns.Add("Probability");

            this.dataGridServiceTime.DataSource = serviceTime;
        
        }

        private void rdBtnExpService_CheckedChanged(object sender, EventArgs e)
        {
            this.dataGridServiceTime.Enabled = false;
            this.txtMinService.Enabled = false;
            this.txtMaxService.Enabled = false;
            this.txtExpMeanService.Enabled = true;
        }

        private void rdBtnUniformService_CheckedChanged(object sender, EventArgs e)
        {
            this.txtMinService.Enabled = true;
            this.txtMaxService.Enabled = true;
            this.txtExpMeanService.Enabled = false;
            this.dataGridServiceTime.Enabled = false;
        }

        public List<IDistribution> ServiceTimeDist
        {
            get
            {
                return serviceTimeDist;
            }
        }

        private void SaveServerData()
        {
            if (this.rdBtnExpService.Checked)
            {
                if (this.txtExpMeanService.Text == string.Empty)
                {
                    MessageBox.Show("Please specify a mean value for the exponential distribution of the service time.");
                }
                else
                {
                    ExponentialDistribution exponential = new ExponentialDistribution(float.Parse(this.txtExpMeanService.Text));
                    serviceTimeDist.Add(exponential);
                    this.rdBtnExpService.Checked = false;
                    this.txtExpMeanService.Clear();
                }
            }
            else
                if (this.rdBtnUniformService.Checked)
                {
                    if (this.txtMinService.Text == string.Empty || this.txtMaxService.Text == string.Empty)
                    {
                        MessageBox.Show("Please specify a range for the uniform distribution of the service time.");
                    }
                    else
                    {
                        UniformDistribution uniform = new UniformDistribution(float.Parse(this.txtMinService.Text), float.Parse(txtMaxService.Text));
                        this.serviceTimeDist.Add(uniform);
                        this.rdBtnUniformService.Checked = false;
                        this.txtMaxService.Clear();
                        this.txtMinService.Clear();
                    }
                }
                else
                {
                    List<float> serviceValues = new List<float>();
                    List<float> serviceProbabilities = new List<float>();
                    if (this.dataGridServiceTime.RowCount < 1)
                    {
                        MessageBox.Show("Please specify discrete values and their probabilities for service time.");
                    }
                    else
                    {
                        for (int i = 0; i < this.dataGridServiceTime.RowCount - 1; i++)
                        {
                            if (this.dataGridServiceTime.Rows[i].Cells[0].Equals(null) || this.dataGridServiceTime.Rows[i].Cells[1].Equals(null))
                            {
                                MessageBox.Show("Please specify discrete values and their probabilities for service time.");
                                break;
                            }
                            else
                            {
                                serviceValues.Add(float.Parse(this.dataGridServiceTime.Rows[i].Cells[0].Value.ToString()));
                                serviceProbabilities.Add(float.Parse(this.dataGridServiceTime.Rows[i].Cells[1].Value.ToString()));
                            }
                        }
                        DiscreteDistribution discrete = new DiscreteDistribution(serviceValues, serviceProbabilities);
                        this.serviceTimeDist.Add(discrete);
                        //DataTable emptyTable = new DataTable();
                        this.dataGridServiceTime.DataSource = null;
                        this.dataGridServiceTime.Enabled = false;
                        this.rdDiscrete.Checked = false;
                    }

                }
        }

        int i = 1;
        private void btnSave_Click(object sender, EventArgs e)
        {
            this.SaveServerData();
            lblServerNumber.Text = "Server: " + (i + 1).ToString();
            if (i < servers)
            {
                i++;
            }
            else
            {
                this.Close();
            }
        }

    }
}
